import {p} from "./ajax.js"
var a = decodeURI(window.location.search);
console.log(a);
var b = a.split("=");
console.log(b);
var name = b[b.length-1];
console.log(name);

var all = document.getElementById("iot-all");
p("https://api-hmugo-web.itheima.net/api/public/v1/goods/search?query="+name).then((res)=>{
        return res.message.goods;
     }).then((goods)=>{
        console.log(goods);
        for(var i =0;i<goods.length;i++){
         let ii = i;
            var div = document.createElement("div");
            all.appendChild(div);
            div.setAttribute("id","iot-div"+i);
            document.getElementById("iot-div"+i).addEventListener("click",fn1);
            div.style.border = "solid 1px";
            div.style.borderColor = "antiquewhite"
            div.style.display = "flex";
            div.style.width = "100%";

            var div1 = document.createElement("div");
            div.appendChild(div1);
            var img = document.createElement("img");
            div1.appendChild(img);
            img.src = goods[i].goods_small_logo;
           
            div1.style.width = "30%";
            img.style.width = "100%";
            img.style.aspectRatio = "1/1";

            var div2 = document.createElement("div");
            div.appendChild(div2);
            div2.style.width = "70%"
            div2.style.justifyContent = "space-between"


            var div3 = document.createElement("div");
            div2.appendChild(div3);
            var div4 = document.createElement("div");
            div2.appendChild(div4);
            div3.innerHTML = goods[i].goods_name;
            
            div4.innerHTML ="￥"+ goods[i].goods_price+".00";
            div4.style.color = "red";

            function fn1(){
               var goods_id = goods[ii].goods_id;
               console.log(goods_id);
               window.open("商品详情.html?goods_id="+goods_id,"_self")//传参goods_id,_self此窗口打开页面
            }
            
        }

     })
   

   